package com.brillilab.starter.controller.aio.topics;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.topics.Topic;
import com.brillilab.domain.po.topics.TopicDirection;
import com.brillilab.domain.po.topics.TopicMember;
import com.brillilab.domain.po.topics.TopicTarget;
import com.brillilab.domain.vo.lab.MemberInfoVo;
import com.brillilab.domain.vo.topics.TopicExperimentResultVo;
import com.brillilab.domain.vo.topics.TopicTargetAddVo;
import com.brillilab.domain.vo.topics.TopicTargetEditVo;
import com.brillilab.domain.vo.topics.TopicTargetsVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.topics.ITopicDirectionService;
import com.brillilab.service.core.topics.ITopicMemberService;
import com.brillilab.service.core.topics.ITopicService;
import com.brillilab.service.core.topics.ITopicTargetService;
import com.brillilab.service.logic.topics.TopicLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;

import cn.hutool.core.collection.CollUtil;

/**
 * 课题Controller
 * 
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/topicTarget")
public class TopicTargetController {
	@Resource
	private UsersLogic usersLogic;
	@Autowired
	private TopicLogic topicLogic;
	@Resource
	private ITopicService topicService;
	@Resource
	private ITopicTargetService topicTargetService;
	@Resource
	private ITopicDirectionService topicDirectionService;
	@Resource
	private ITopicMemberService topicMemberService;

	/**
	 * 新建实验流
	 * 
	 * @return
	 */
	@PostMapping("/add")
	public ResponseVo targetAdd(@RequestBody TopicTargetAddVo addVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		addVo.setLabId(userInfo.getLabId());

		TopicDirection direction = topicDirectionService.getById(addVo.getTopicDirectionId());
		Topic topic = topicService.selectById(direction.getTopicId());
		boolean isPower = topicLogic.topicEditPower(topic, userInfo);
		if (!isPower) {
			TopicMember topicMember = topicMemberService.getTopicMember(direction.getTopicId(),
					userInfo.getLabMemberId());
			isPower = topicMember != null;
		}
		Assert.isTrue(isPower, "你没有权限操作该课题");
		TopicTarget topicTarget = topicTargetService.targetAdd(addVo);
		return ResponseVo.success(topicTarget);
	}

	/**
	 * 编辑实验流
	 * 
	 * @return
	 */
	@PostMapping("/edit")
	public ResponseVo targetEdit(@RequestBody TopicTargetEditVo editVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {

		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		TopicTarget target = topicTargetService.selectById(editVo.getTopicTargetId());
		Topic topic = topicService.selectById(target.getTopicId());
		boolean isPower = topicLogic.topicEditPower(topic, userInfo);
		Assert.isTrue(isPower, "你没有权限操作该课题");

		target.setName(editVo.getName());
		topicTargetService.updateById(target);
		return ResponseVo.success();
	}

	/**
	 * 删除实验流
	 * 
	 * @return
	 */
	@GetMapping("/delete")
	public ResponseVo targetDelete(@RequestParam Long topicTargetId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		TopicTarget target = topicTargetService.selectById(topicTargetId);
		Topic topic = topicService.selectById(target.getTopicId());
		boolean isPower = topicLogic.topicEditPower(topic, userInfo);
		Assert.isTrue(isPower, "你没有权限操作该课题");
		topicTargetService.delete(target);
		return ResponseVo.success();
	}

	/**
	 * 获取实验流列表
	 * 
	 * @return
	 */
	@Deprecated
	@GetMapping("/list")
	public ResponseVo targetList(@RequestParam Long topicDirectionId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		directionIsDelete(topicDirectionId);
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		TopicTargetsVo targetsVo = topicLogic.getTargetList(topicDirectionId, userInfo);
		return ResponseVo.success(targetsVo.getTargetList());
	}

	/**
	 * 获取实验流列表对象（包含实验内容名称和Figure）
	 * 
	 * @return
	 */
	@GetMapping("/listVo")
	public ResponseVo targetListVo(@RequestParam Long topicDirectionId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		directionIsDelete(topicDirectionId);
		
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		TopicTargetsVo targetsVo = topicLogic.getTargetList(topicDirectionId, userInfo);
		return ResponseVo.success(targetsVo);
	}

	//校验研究内容是否删除
	private void directionIsDelete(Long topicDirectionId) {
		TopicDirection direction = topicDirectionService.getById(topicDirectionId);
		Assert.isTrue(direction!=null && BoolEnum.FALSE.getValue().equals(direction.getIsDelete()), "研究内容已被删除");
		Topic topic = topicService.selectById(direction.getTopicId());
		Assert.isTrue(topic!=null && BoolEnum.FALSE.getValue().equals(topic.getIsDelete()), "课题已被删除");
	}
		
	/**
	 * 获取实验流的不同计划日期实验结果
	 * 
	 * @return
	 */
	@GetMapping("/planResult")
	public ResponseVo targetPlanResult(@RequestParam Long targetGroupId, Long targetId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		List<TopicExperimentResultVo> experimentResultList = topicLogic.getExperimentResultList(targetGroupId,
				targetId);
		return ResponseVo.success(experimentResultList);
	}

	/**
	 * 获取实验流下的实验执行人
	 * 
	 * @return
	 */
	@GetMapping("/memberList")
	public ResponseVo getTargetMemberList(@RequestParam Long topicTargetId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		List<MemberInfoVo> memberList = topicTargetService.getTargetMemberList(topicTargetId);
		return ResponseVo.success(memberList);
	}

	/**
	 * 初始化颜色
	 * 
	 * @return
	 */
	@GetMapping("/color")
	@Deprecated
	public ResponseVo color() {
		List<String> colorList = CollUtil.toList("#EB5444", "#F6AC2D", "#00CB7C", "#70AACE", "#797BDA", "#B763A1",
				"#BAA753");
		List<TopicTarget> list = topicTargetService.getTargetAllList();
		if (CollUtil.isNotEmpty(list)) {
			Map<Long, List<TopicTarget>> map = list.stream().collect(Collectors.groupingBy(TopicTarget::getTopicId));
			for (Long topicDirectionId : map.keySet()) {
				List<TopicTarget> list2 = map.get(topicDirectionId);
				if (CollUtil.isNotEmpty(list2)) {
					for (int i = 0; i < list2.size(); i++) {
						TopicTarget target = list2.get(i);
						int index = i % colorList.size();
						target.setColor(colorList.get(index));
						topicTargetService.updateById(target);
					}
				}
			}
		}
		return ResponseVo.success();
	}

}
